package com.appistry.api.rest.impl;

import java.net.InetAddress;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.UsernamePasswordCredentials;

import com.appistry.api.rest.domain.Credentials;

public class HttpRestConnection extends AbstractRestConnection {
	private static final String PROTOCOL = "http";
	private static final int DEFAULT_PORT = 80;
	private HostConfiguration hostConfiguration = new HostConfiguration();
	private UsernamePasswordCredentials credentials;

	public HttpRestConnection(InetAddress address, int port, Credentials credentials) {
		super();
		
		//set host configuration
		hostConfiguration.setHost(address.getHostAddress(), port, PROTOCOL);
		
		//set credentials and preemptive auth				
		if(credentials!=null){
			this.credentials = new UsernamePasswordCredentials(credentials.getUser(), credentials.getPassword());
			getHttpClientParams().setAuthenticationPreemptive(true);
		}
	}

	public HttpRestConnection(InetAddress address, Credentials credentials) {
		this(address, DEFAULT_PORT, credentials);
	}

	public HttpRestConnection(InetAddress address, int port) {
		this(address, port, null);
	}

	public HttpRestConnection(InetAddress address) {
		this(address, DEFAULT_PORT, null);
	}

	protected HostConfiguration getHostConfiguration() {
		return hostConfiguration;
	}

	protected void setHostConfiguration(HostConfiguration hostConfiguration) {
		this.hostConfiguration = hostConfiguration;
	}

	protected UsernamePasswordCredentials getUsernamePasswordCredentials() {
		return credentials;
	}

	protected void setUsernamePasswordCredentials(UsernamePasswordCredentials credentials) {
		this.credentials = credentials;
	}

}
